<template>
  <div class="myCourse" style="padding-top: 10px">
    <el-table :data="tableData" border style="width: 100%; padding: 20px">
      <el-table-column prop="name" label="课程名称"> </el-table-column>
      <el-table-column prop="teacher" label="老师"> </el-table-column>
      <el-table-column prop="info" label="课程内容"> </el-table-column>
      <el-table-column prop="peopleNumber" label="报名人数"> </el-table-column>
      <el-table-column prop="operation" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="deleteCourse(scope.$index)"
            >取消学习</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { schools, courses } from "../../../public/schoolData.js";
export default {
  name: "myCourse",
  data: function () {
    return {
      id: JSON.parse(sessionStorage.getItem("studentInfo")).studentId,
      delCourse: "",
      tableData: [],
    };
  },
  created() {
    this.round();
  },
  computed: {
    // courseList: () => {
    //   return schools.find((data) => {
    //     return (
    //       data.id == JSON.parse(sessionStorage.getItem("studentInfo")).studentId
    //     );
    //   }).inCourseList;
    // },
  },
  methods: {
    round() {
      this.tableData = [];
      schools
        .find((data) => {
          return data.id == this.id;
        })
        .inCourseList.forEach((data) => {
          let queryResult = courses.some((item) => {
            return data.id == item.id;
          });
          if (queryResult) {
            this.tableData.push(data);
          }
        });
    },
    //点击取消学习，删除当前课程，并将修改后的数据更改到schoolData.js当中
    deleteCourse(index) {
      this.$confirm("此操作将取消对该课程的报名, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          schools
            .find((data) => {
              return (
                data.id ==
                JSON.parse(sessionStorage.getItem("studentInfo")).studentId
              );
            })
            .inCourseList.splice(index, 1);

          this.$message({
            type: "success",
            message: "取消成功!",
            //取消课程
            //并将修改后的数据更新到schoolData.js中
          });
          this.round();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消该操作",
          });
        });
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
</style>
